package chu.dejian.db;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;

import com.sam.utils.L;

import chu.dejian.entity.RecordEntity;

public class RecordDao {
	
	public static final String RECORD_TABLE = "blood_pressure_record";
	public static final String KEY_ROWID = "_id";
	public static final String KEY_SYSTOLIC = "systolic";  // 心脏收缩压
	public static final String KEY_DIASTOLIC = "diastolic"; // 心脏舒张压
	public static final String KEY_PULSE = "pulse"; // 脉搏
	public static final String KEY_PULSE_STATE = "pulseState";
	public static final String KEY_MESSAGE = "message";
	public static final String KEY_NOTE = "note";
	public static final String KEY_MEASURE_TIME = "measure_time";
	public static final String KEY_IS_UPLOADED = "is_uploaded";

	public static final String RECORD_TABLE_CREATE = "CREATE TABLE "
			+ RECORD_TABLE + " ("
			+ KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ KEY_SYSTOLIC + " INTEGER, "
			+ KEY_DIASTOLIC + " INTEGER, "
			+ KEY_PULSE + " INTEGER, "
			+ KEY_PULSE_STATE + " TEXT, "
			+ KEY_MESSAGE + " TEXT, "
			+ KEY_NOTE + " TEXT, "
			+ KEY_MEASURE_TIME + " DATETIME DEFAULT (datetime('now','localtime')), "
			+ KEY_IS_UPLOADED + " BOOLEAN DEFAULT FALSE);";

	public long insertRecord(RecordEntity recordEntity) {
		if(recordEntity == null) return -1;
		String time = recordEntity.dateTime == null ? "(datetime('now','localtime'))" : recordEntity.dateTime;
		Cursor cursor = DBHelper.getInstance().getDatabase().query(RECORD_TABLE,null,	KEY_MEASURE_TIME+" = ?", new String[]{time}, null, null, null);
		if(cursor != null && cursor.moveToFirst()) return -1;
		L.d("插入了一条数据到数据库，记录为\n"+recordEntity.toString());
		return DBHelper.getInstance().getDatabase().insert(RECORD_TABLE, null, createContentValues(recordEntity));
	}

	public boolean updateRecord(RecordEntity recordEntity) {
		return DBHelper.getInstance().getDatabase().update(RECORD_TABLE, createContentValues(recordEntity), KEY_ROWID + "=" + recordEntity.recordId, null) > 0;
	}

	public boolean deleteRecord(long rowId) {
		return DBHelper.getInstance().getDatabase().delete(RECORD_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public List<RecordEntity> getAllUnuploadedRecord(){
		return getRecords(KEY_IS_UPLOADED + " = 0");
	}

	public List<RecordEntity> getAllRecord(){
		return getRecords(null);
	}

	public List<RecordEntity> getRecords(String selection) {
		Cursor cursor = DBHelper.getInstance().getDatabase().query(RECORD_TABLE,null,	selection, null, null, null, KEY_MEASURE_TIME + " ASC");
		List<RecordEntity> recordEntityList = new ArrayList<>();
		while (cursor != null && cursor.moveToNext()) {
			RecordEntity recordEntity = new RecordEntity();
			recordEntity.recordId = cursor.getInt(cursor.getColumnIndex(KEY_ROWID));
			recordEntity.systolic = cursor.getInt(cursor.getColumnIndex(KEY_SYSTOLIC));
			recordEntity.diastolic = cursor.getInt(cursor.getColumnIndex(KEY_DIASTOLIC));
			recordEntity.pulse = cursor.getInt(cursor.getColumnIndex(KEY_PULSE));
			recordEntity.pulseState = cursor.getInt(cursor.getColumnIndex(KEY_PULSE_STATE));
			recordEntity.message = cursor.getString(cursor.getColumnIndex(KEY_MESSAGE));
			recordEntity.note = cursor.getString(cursor.getColumnIndex(KEY_NOTE));
			recordEntity.dateTime = cursor.getString(cursor.getColumnIndex(KEY_MEASURE_TIME));
			recordEntity.isUploaded = cursor.getInt(cursor.getColumnIndex(KEY_IS_UPLOADED)) > 0;
			recordEntityList.add(recordEntity);
		}
		cursor.close();
		return recordEntityList;
	}

	public void setRecordUploaded(int recordId){
		ContentValues contentValues = new ContentValues();
		contentValues.put(KEY_IS_UPLOADED,true);
		DBHelper.getInstance().getDatabase().update(RECORD_TABLE,contentValues,KEY_ROWID + "=" + recordId, null);
	}

	public RecordEntity getRecord(long rowId) throws SQLException {
		Cursor cursor = DBHelper.getInstance().getDatabase().query(true, RECORD_TABLE,
				new String[]{KEY_ROWID,KEY_SYSTOLIC,KEY_DIASTOLIC,KEY_PULSE,KEY_PULSE_STATE,KEY_MESSAGE,KEY_NOTE,KEY_MEASURE_TIME},
				KEY_ROWID + "=" + rowId, null, null, null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			RecordEntity recordEntity = new RecordEntity();
			recordEntity.recordId = cursor.getInt(cursor.getColumnIndex(KEY_ROWID));
			recordEntity.systolic = cursor.getInt(cursor.getColumnIndex(KEY_SYSTOLIC));
			recordEntity.diastolic = cursor.getInt(cursor.getColumnIndex(KEY_DIASTOLIC));
			recordEntity.pulse = cursor.getInt(cursor.getColumnIndex(KEY_PULSE));
			recordEntity.pulseState = cursor.getInt(cursor.getColumnIndex(KEY_PULSE_STATE));
			recordEntity.message = cursor.getString(cursor.getColumnIndex(KEY_MESSAGE));
			recordEntity.note = cursor.getString(cursor.getColumnIndex(KEY_NOTE));
			recordEntity.dateTime = cursor.getString(cursor.getColumnIndex(KEY_MEASURE_TIME));
			recordEntity.isUploaded = cursor.getInt(cursor.getColumnIndex(KEY_IS_UPLOADED)) > 0;
			return recordEntity;
		}
		return null;
	}

	private ContentValues createContentValues(RecordEntity recordEntity) {
		ContentValues values = new ContentValues();
		values.put(KEY_SYSTOLIC, recordEntity.systolic);
		values.put(KEY_DIASTOLIC, recordEntity.diastolic);
		values.put(KEY_PULSE, recordEntity.pulse);
		values.put(KEY_PULSE_STATE,recordEntity.pulseState);
		values.put(KEY_MESSAGE,recordEntity.message);
		values.put(KEY_NOTE,recordEntity.note);
		values.put(KEY_MEASURE_TIME, recordEntity.dateTime);
		values.put(KEY_IS_UPLOADED,recordEntity.isUploaded);
		return values;
	}
}
